jQuery(function($) {
    var doc = document,
        $doc = $(doc);

    var form = {
        moblie: false,
        paw: false,
        msg: false
    },
        timer = {
            count:60,
            index: 0
    }

    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    //清除错误信息
    function clearErrMessage(input, err) {
        $doc.on("focus", input, function() {
            $(err).css({"visibility": "hidden"});
        });
    }
    //是否为空
    function empty(val,key,err) {
        if (!utils.empty(val)) {
            form[key] = true;
        } else {
            form[key] = false;
            utils.errMessage(err);
        }
    }

    //是否为数字
    function isNumber(val,key,err) {
        if (utils.isNumber(val)) {
             form[key] = true;
        } else {
            form[key] = false;
            alert(err);
        }
    }

    //短信验证码 -start-

    function setTimer() {
        var btn = $(".msgBtn");

        if (!form.msg) {
            return;
        }

        //禁用表单
        btn[0].setAttribute("disabled", "disabled");
        timer.count--;

        clearInterval(timer.index);
        timer.index = setInterval(function() {
            if (timer.count <= 0) {
                clearInterval(timer.index);
                timer.count = 60;
                btn.html("获取验证码");
                btn[0].removeAttribute("disabled");
                return;
            }

            btn.html((timer.count--) + " 秒后重新发送");
        }, 1000);
    }

    function checkMessage(val) {
        // empty(val,"msg",".msg_error");
        // isNumber(val, "msg", "短信验证码必须为数字！");
        checkMoblieSec($(".phone input").val(), ".moblie", ".moblie_error");
        var phone = $(".phone input").val();
        if (form.moblie) {
            form.msg = true;
            $.ajax({
                type:"post",
                url:getVerifyCodeByPhoneUrl,
                dataType:"json",
                data:{"phone":phone},
                success:function(obj){
                    if(obj.level == "success"){
                        setTimer(); //手机号正确时倒计时
                        $(".msg_error").css({"visibility": "hidden"});
                    }
                },
                error:function(obj){
                    var datajson = obj.responseJSON;
                    noty({type:datajson.level,text:datajson.message,timeout:600});
                }
            });
        } else {
            form.msge = false;
            alert("请填写正确的手机号！");
            return;
        }
    }
    function checkMsgSec(val) {
        if (!utils.empty(val)) {
            form.msg = true;
        } else {
            form.msg = false;
            utils.errMessage(".msg_error");
            return;
        }

        if (utils.isNumber(val)) {
            form.msg = true;
        } else {
            form.msg = false;
            alert("请填写正确的手机验证码！");
            return;
        }

        if (val.length === 4) {
            form.msg = true;
        } else {
            form.msg = false;
            alert("请填写正确的手机验证码！");
            return;
        }
    }

    $doc.on("click", ".msgBtn", function() {
        var val = $(".msg input").val();
        checkMessage(val);
       
    });

    //短信验证码 -end-

    //验证手机号 -start-
    function checkMoblie(val, key, err) {
        if (!utils.empty(val)) {
            form.moblie = true;
        } else {
            form.moblie = false;
            utils.errMessage(err);
            return;
        }

        //手机号是否为数值
        if (utils.checkMoblie($(".phone input"))) {
            form.moblie = true;
        } else {
            form.moblie = false;
            alert("请填写正确的手机号码！");
        }
    }
    function checkMoblieSec(val, key, err) {
        if (!utils.empty(val)) {
            form.moblie = true;
        } else {
            form.moblie = false;
            utils.errMessage(err);
            return;
        }

        //手机号是否为数值
        if (utils.checkMoblie($(".phone input"))) {
            form.moblie = true;
        } else {
            form.moblie = false;
            // alert("请填写正确的手机号码！");
        }
    }
    //验证手机号 -end-

    //验证密码 -start-
    function checkPaw(val, key, err) {
        if (!utils.empty(val)) {
            form.paw = true;
        } else {
            form.paw = false;
            utils.errMessage(err);
        }
    }
    //验证密码 -end-

    //提交表单 -start-

    $doc.on("click", ".submit", function() {
        checkMoblie($(".phone input").val(), ".moblie", ".moblie_error");
        checkPaw($(".paw input").val(), "paw", ".paw_error");
        checkMsgSec($(".msg input").val());
        var phone = $(".phone input").val();
        var code  = $(".msg input").val();
        var pwd   = $(".paw input").val();
        if (form.moblie &&
            form.paw &&
            form.msg) {
            //do something
            $.ajax({
                type:"post",
                url:resetPassword,
                dataType:"json",
                data:{"phone":phone,'verification_code':code,'password':pwd},
                success:function(obj){
                    if(obj.level == "success"){
                        noty({text:obj.message,type:obj.level,
                            callback:{
                                afterClose:function(){
                                    window.location.href=loginPage;
                                }
                            }
                        });
                    }
                },
                error:function(obj){
                    var datajson = obj.responseJSON;
                    noty({type:datajson.level,text:datajson.message,timeout:600});
                }
            });
        }
       
    });

    /*拆分字符串*/
    function createElm(elm) {
        return doc.createElement(elm);
    }
    function createText(text) {
        return doc.createTextNode(text);
    }
    function getUserCount(param) {
        if (typeof param !== "string") {
            return false;
        }

        var fragment = doc.createDocumentFragment(),
            count = doc.querySelector(".count ul"),
            li = null,
            em = null,
            text = "";

        for (var i = 0; i < param.length; i++) {
            li = createElm("li");
            em = createElm("em");
            text = createText(param.substr(i,1));
            em.appendChild(text);
            li.appendChild(em);
            fragment.appendChild(li);
        }

        count.appendChild(fragment);
    }
    getUserCount(userCount);

    //提交表单 -end-
    clearErrMessage(".msg input", ".msg_error");
    clearErrMessage(".phone input", ".moblie_error");
    clearErrMessage(".paw input", ".paw_error");
});